import request from '@/utils/request'

// 用户登录
export function login(data) {
  return request({
    url: '/user/login',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: new URLSearchParams({
      username: data.username,
      password: data.password
    })
  }).then(response => {
    console.log('Login API response:', response);
    // 登录成功后保存用户信息
    if (response.code === 200 && response.data?.user) {
      localStorage.setItem('userInfo', JSON.stringify(response.data.user))
    }
    return response;
  });
}

// 获取用户信息
export function getUserInfo() {
  return request({
    url: '/user/info',
    method: 'get'
  })
}

// 用户注册
export function register(data) {
  return request({
    url: '/user/register',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  })
} 